<template>
  <header :class="['header', { scrolled: isScrolled }]">
    <div class="logo">
      <router-link to="/">
        <img src="https://static.leiqichao.cn/logo.png" alt="山海 Logo" class="logo-image" @load="initHeader">
        <span class="logo-text">山海</span>
      </router-link>
    </div>

    <nav class="nav-container">
      <div class="nav-scroll-wrapper">
        <router-link v-for="(nav, index) in navItems" :key="index" :to="nav.path" class="nav-item"
          :ref="nav.text === '简历' ? 'resumeButton' : null">
          <i :class="nav.icon"></i>
          <span class="nav-text">{{ nav.text }}</span>
        </router-link>

        <div v-if="showResumePrompt" class="guide-arrow">
          <div class="arrow-body"></div>
          <div class="arrow-head"></div>
        </div>

        <div v-if="showResumePrompt" class="prompt-bubble" @click="closePrompt">
          <span>👆面试官看这里！点击简历</span>
          <button class="close-btn">&times;</button>
        </div>
      </div>
    </nav>
  </header>
</template>

<script>
export default {
  name: 'AppHeader',
  data() {
    return {
      isScrolled: false,
      showResumePrompt: true,
      timeoutId: null,  // 新增定时器ID
      navItems: [
        { path: '/', icon: 'fas fa-home', text: '首页' },
        { path: '/articles', icon: 'fas fa-file-alt', text: '文章' },
        { path: '/resume', icon: 'fas fa-file-invoice', text: '简历' },
        { path: '/messages', icon: 'fas fa-comment', text: '留言' },
        { path: '/about', icon: 'fas fa-info-circle', text: '关于' }
      ]
    };
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll);
    window.addEventListener('resize', this.handleResize);
    window.addEventListener('scroll', this.closePrompt);

    // 新增5秒定时关闭
    this.timeoutId = setTimeout(() => {
      this.showResumePrompt = false;
    }, 2000);
  },
  beforeUnmount() {
    window.removeEventListener('scroll', this.handleScroll);
    window.removeEventListener('resize', this.handleResize);
    window.removeEventListener('scroll', this.closePrompt);

    // 清除定时器
    if (this.timeoutId) {
      clearTimeout(this.timeoutId);
    }
  },
  methods: {
    handleScroll() {
      this.isScrolled = window.scrollY > 50;
    },
    handleResize() {
      this.calculateResponsiveLayout();
    },
    initHeader() {
      this.$nextTick(() => {
        this.calculateResponsiveLayout();
      });
    },
    calculateResponsiveLayout() {
      const logoWidth = document.querySelector('.logo').offsetWidth;
      const headerWidth = document.querySelector('.header').offsetWidth;
      const availableSpace = headerWidth - logoWidth - 40;
      this.$el.querySelector('.nav-container').style.maxWidth = `${availableSpace}px`;
    },
    closePrompt() {
      this.showResumePrompt = false;
      // 清除定时器避免重复执行
      if (this.timeoutId) {
        clearTimeout(this.timeoutId);
        this.timeoutId = null;
      }
    }
  }
};
</script>

<style scoped>
/* 基础样式（保持原有） */
.header {
  --logo-width: 180px;
  --nav-gap: 0.75rem;
  --icon-size: 1.2rem;
  --text-opacity: 0.9;

  background: rgba(22, 46, 53, 0.95);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 5%;
  position: fixed;
  width: 100%;
  top: 0;
  z-index: 1000;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.logo {
  flex: 0 0 auto;
  min-width: var(--logo-width);
}

.logo a {
  display: flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
}

.logo-image {
  height: 42px;
  width: auto;
  transition: transform 0.3s ease;
}

.logo-text {
  font-family: 'Microsoft YaHei', sans-serif;
  font-size: 1.8rem;
  color: #e0f2fe;
  font-weight: 700;
}

/* 导航核心样式 */
.nav-container {
  flex: 1 1 auto;
  min-width: 0;
  max-width: calc(100% - var(--logo-width) - 5%);
  margin-left: auto;
  position: relative;
}

.nav-scroll-wrapper {
  display: flex;
  justify-content: flex-end;
  gap: var(--nav-gap);
  overflow: visible;
  width: 100%;
}

.nav-item {
  display: inline-flex;
  align-items: center;
  padding: 0.8rem;
  color: rgba(224, 242, 254, var(--text-opacity));
  font-weight: 500;
  transition: all 0.3s ease;
  white-space: nowrap;
  flex-shrink: 0;
}

.nav-item i {
  font-size: var(--icon-size);
  min-width: calc(var(--icon-size) + 4px);
  text-align: center;
}

.nav-text {
  margin-left: 0.5rem;
  transition: opacity 0.3s ease;
}

/* 交互状态 */
.nav-item:hover {
  color: #ffffff;
  transform: translateY(-2px);
}

.nav-item.router-link-exact-active {
  color: #87CEEB;
  font-weight: 600;
}

/* 新增指引样式 */
.guide-arrow {
  position: absolute;
  right: 120px;
  top: -40px;
  z-index: 1001;
}

.arrow-body {
  width: 80px;
  height: 2px;
  background: #FFD700;
  position: relative;
  animation: arrowPulse 1.5s infinite;
}

.arrow-head {
  position: absolute;
  right: -6px;
  top: -4px;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
  border-left: 12px solid #FFD700;
  transform: rotate(135deg); /* 调整箭头方向 */
}

.prompt-bubble {
  position: absolute;
  right: 30px;
  bottom: -60px;
  /* 改为底部定位 */
  background: #FFD700;
  color: #2c3e50;
  padding: 12px 20px;
  border-radius: 25px;
  display: flex;
  align-items: center;
  gap: 10px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  z-index: 1001;
  animation: bubbleFloat 2s ease-in-out infinite;
}

.close-btn {
  background: none;
  border: none;
  color: #2c3e50;
  font-size: 1.2em;
  cursor: pointer;
  padding: 0 5px;
}

/* 动画效果 */
@keyframes arrowPulse {
  0% {
    opacity: 0.4;
  }

  50% {
    opacity: 1;
  }

  100% {
    opacity: 0.4;
  }
}

@keyframes headPulse {
  0% {
    transform: rotate(-45deg) scale(1);
  }

  50% {
    transform: rotate(-45deg) scale(1.3);
  }

  100% {
    transform: rotate(-45deg) scale(1);
  }
}

@keyframes bubbleFloat {
  0% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-8px);
  }

  100% {
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 1200px) {
  :root {
    --nav-gap: 0.6rem;
  }
}

@media (max-width: 992px) {
  .header {
    padding: 0.5rem;
  }

  .nav-text {
    opacity: 0;
    width: 0;
    margin: 0;
  }

  .nav-item {
    padding: 0.6rem;
  }
}

@media (max-width: 768px) {
  :root {
    --icon-size: 0.4rem;
    --nav-gap: 1rem;
  }

  .logo-text {
    font-size: 1.6rem;
  }

  .guide-arrow {
    position: absolute;
    right: 120px;
    bottom: -30px;
    /* 同步修改箭头位置 */
    z-index: 1001;
  }

  .arrow-body {
    width: 80px;
    height: 2px;
    background: #FFD700;
    position: relative;
    animation: arrowPulse 1.5s infinite;
  }

  .prompt-bubble {
    right: 10px;
    padding: 8px 15px;
    font-size: 0.9em;
  }
}

@media (max-width: 576px) {
  :root {
    --nav-gap: 0.8rem;
    --icon-size: 1.3rem;
  }

  .logo {
    min-width: 160px;
  }

  .logo-image {
    height: 36px;
  }

  .nav-item {
    padding: 0.5rem;
  }
}

@media (max-width: 480px) {
  :root {
    --nav-gap: 0.3rem;
  }

  .header {
    padding: 0.8rem 2%;
  }

  .logo-text {
    font-size: 1.4rem;
  }
}

@media (max-width: 360px) {
  :root {
    --nav-gap: 0.5rem;
    --icon-size: 1.2rem;
  }

  .nav-item {
    padding: 0.4rem;
  }
}

/* 滚动状态 */
.header.scrolled {
  background: rgba(12, 36, 43, 0.98);
  padding: 0.8rem 5%;

  .logo-image {
    height: 36px;
  }

  .logo-text {
    font-size: 1.6rem;
  }

  @media (max-width: 768px) {
    padding: 0.8rem 3%;
  }
}
</style>